<?php
namespace app\index\controller;
use think\Db;
use think\Session;
use think\Request;
class Adminuser extends \app\index\controller\Common
{
	public function user_list()
	{
		$page=Request::instance()->get('page');
		$limit=Request::instance()->get('limit');
		//搜索条件
		$username=Request::instance()->get('username');
		$telphone=Request::instance()->get('telphone');
		$email=Request::instance()->get('email');
		$map = array();
		$admin_user = array();
		if ($username!="") {
			$map['username'] = ['like',"%$username%"];
		}
		if ($telphone!="") {
			$map['telphone'] = ['like',"%$telphone%"];
		}
		if ($email!="") {
			$map['email'] = ['like',"%$email%"];
		}
		$admin_user_arr=Db::table('admin_user')->where($map)->page("$page,$limit")->select();
		
		foreach ($admin_user_arr as $key => $value) {
			$id=$value['id'];
			//获取用户组信息
			$role=Db::table('auth_group_access')->field('group.id,group.title')
			->alias('access')
			->join('auth_group group','access.group_id = group.id')
			->where('access.uid',$id)
			->find();
			$admin_user_arr[$key]['role']=$role;
			//获取用户菜单组信息
			$menu_role=Db::table('menu_group_access')->field('group.id,group.title')
			->alias('access')
			->join('menu_group group','access.group_id = group.id')
			->where('access.uid',$id)
			->find();
			$admin_user_arr[$key]['menu_role']=$menu_role;
		}


		//查询用户组
		$role=Request::instance()->get('role');
		if ($role!="") {
			foreach ($admin_user_arr as $key => $value) {
				if ($value['role']['id']==$role) {
					$admin_user_arr[$key]['role']=$value['role']['title'];
					$admin_user[]=$admin_user_arr[$key];
				}
			}
		}else{
			foreach ($admin_user_arr as $key => $value) {
				$admin_user_arr[$key]['role']=$value['role']['title'];
				$admin_user_arr[$key]['menu_role']=$value['menu_role']['title'];
			}
			$admin_user=$admin_user_arr;
		}


		$arr['code']='0';
		$arr['msg']='';
		$arr['count']=Db::table('admin_user')->where($map)->count();
		$arr['data']=$admin_user;
		return $arr;
		//$this->result($admin_user,0,'获取成功');
	}
	public function add()
	{
		$data = array();
		$data['username']=Request::instance()->post('username');
		$data['password']=md5(Request::instance()->post('password'));
		$data['telphone']=Request::instance()->post('telphone');
		$data['email']=Request::instance()->post('email');
		if (Request::instance()->has('status','post')) {
			$data['status']=Request::instance()->post('status');
		}else{
			$data['status']='0';
		}
		Db::table('admin_user')->insert($data);
		$id = Db::name('admin_user')->getLastInsID();
		$data = array();
		$data['uid']=$id;
		$data['group_id']=Request::instance()->post('role');
		Db::table('auth_group_access')->insert($data);
		//菜单组
		$data = array();
		$data['uid']=$id;
		$data['group_id']=Request::instance()->post('menu_role');
		Db::table('menu_group_access')->insert($data);
		$this->result($id,0,'提交成功');
	}
	public function edit()
	{
		
		if (Request::instance()->has('id','post')) {
			$id=Request::instance()->post('id');

			$data = array();
			$data['username']=Request::instance()->post('username');
			$data['password']=md5(Request::instance()->post('password'));
			$data['telphone']=Request::instance()->post('telphone');
			$data['email']=Request::instance()->post('email');
			if (Request::instance()->has('status','post')) {
				$data['status']=Request::instance()->post('status');
			}else{
				$data['status']='0';
			}
			Db::table('admin_user')->where('id',$id)->update($data);
			if ($id!='1') {
				$data = array();
				$data['group_id']=Request::instance()->post('role');
				Db::table('auth_group_access')->where('uid',$id)->update($data);
				//菜单组
				$data = array();
				$data['uid']=$id;
				$data['group_id']=Request::instance()->post('menu_role');
				Db::table('menu_group_access')->where('uid',$id)->update($data);
			}
			$this->result($id,0,'提交成功');
		}
	}
	public function del()
	{
		if (Request::instance()->has('id','post')) {
			$id=Request::instance()->post('id');
			$role=Request::instance()->post('role');
			if ($id!="1" && $role!='超级管理员') {
				if (Db::table('admin_user')->delete($id)) {
					$this->result($id,0,'提交成功');
				}else{
					$this->result($id,1,'删除失败');
				}
			}
		}
	}

}